<?php
if (!defined('IN_WONDERSHARE'))
{
die('Hacking attempt');
}
require('./classes/module/module_support_right_recommend.php');
class SupportRightRecommendControl
{
	var $support_right_recommend_m;
	function __construct()
	{
	
		$this->support_right_recommend_m = new SupportRightRecommendModule();
	}

	/**
	* 根据网站类型得到相应的Product Support栏目左侧信息
	* @param   int         $web_type
	* @return  array
	*/
	function  get_recommend_list($web_type)
	{
		$sql = "SELECT two_cate.cat_id , two_cate.cat_title AS two_title , one_cate.cat_title AS one_title ,
		(SELECT cat_title FROM category WHERE cat_id = sup.topic_cat_id  ) AS sup_title,sup.update_user
		,IF(sup.update_time > 0,FROM_UNIXTIME(sup.update_time, '%Y-%m-%d'), '') update_time   FROM  category two_cate
		LEFT JOIN  category one_cate ON one_cate.cat_id = two_cate.parent_id
		LEFT JOIN  support_right_recommend  sup ON sup.p_cat_id = two_cate.cat_id
		WHERE two_cate.cat_type = 2  AND two_cate.cat_web = $web_type AND two_cate.is_delete = 0 AND two_cate.cat_series = 2";
		return $this->support_right_recommend_m->get_info($sql);
	}
	/**
	* 修改侧边链接
	* @param   array         $insert_array    插入到support_right_recommend表的数据 或者修改
	* @return  array
	*/
	function  operate_right_recommend($insert_array)
	{
		$sql = 'SELECT id FROM support_right_recommend WHERE p_cat_id = '.$insert_array['two_cat_id'];
		$id = $GLOBALS['db']->getOne($sql);
		if(empty($id))
		{
			$sql = 'INSERT INTO  support_right_recommend (p_cat_id,topic_cat_id,recommend_article,
			add_time,add_user,update_user,update_time,web_type) VALUES ('.$insert_array['two_cat_id'].','.$insert_array['topic_cat_id'].',
			"'.$insert_array['recommend_article'].'","'.$_SERVER['REQUEST_TIME'].'","'.$_SESSION['AName'].'",
			"'.$_SESSION['AName'].'","'.$_SERVER['REQUEST_TIME'].'","'.$insert_array['web_type'].'")';
		}else
		{
			$sql = 'UPDATE support_right_recommend  SET topic_cat_id = '.$insert_array['topic_cat_id'].',recommend_article = 
			"'.$insert_array['recommend_article'].'",update_user = "'.$_SESSION['AName'].'",update_time = "'.$_SERVER['REQUEST_TIME'].'"
			WHERE p_cat_id = '.$insert_array['two_cat_id'];
		}
		
		return $this->support_right_recommend_m->change_info($sql);
	
	
	}
	/**
	* 操作select_elements当用户选择文章checkbox触发的事件如果在select_elements表不存在则插入记录负责修改，
	* @param   string         $type    操作类型
	* @return  array
	*/
	function  operate_select_elements($type,$cat_id = 0,$start_value = '',$operate_type = '',$operate_id = 0 )
	{
		$user_ip = bindec(decbin(ip2long(real_ip())));
		if($type == 'support_right')
		{
		
			$rows = $this->get_select_elements_id($type,$cat_id);
			$value = empty($rows['e_value'])?$start_value:$rows['e_value'];
			if($operate_type == 'add')
			{
				$value .= empty($value)?$operate_id:','.$operate_id;
			}else
			{
				$e_values = explode(',',$value);
				$keys = array_search($operate_id,$e_values);
				unset($e_values[$keys]);
				$value = implode(',',$e_values);
				unset($e_values);
			}
			if(empty($rows['e_id']))
			{
			$sql = "INSERT INTO select_elements(user_name,user_ip,e_type,add_time,e_value,cat_id) VALUES
			('{$_SESSION['AName']}','{$user_ip}','{$type}','{$_SERVER['REQUEST_TIME']}','{$value}','{$cat_id}')";
			}else
			{
				$sql = 'UPDATE select_elements SET e_value = "'.$value.'" WHERE e_id = '.$rows['e_id'];
			
			
			}
			$GLOBALS['db']->query($sql);
			unset($rows);
		}elseif ($type == 'del_support_right')
		{
			$sql = 'DELETE FROM select_elements WHERE user_name = "'.$_SESSION['AName'].'" AND user_ip =  
			"'.$user_ip.'"  AND e_type = "support_right" AND cat_id = '.$cat_id;
			$GLOBALS['db']->query($sql);
		}
	}
	/**
	* 得到相关类型的选择元素id
	* @param   string          $type    操作类型
	* @return  array
	*/
	function  get_select_elements_id($type,$cat_id)
	{
		$rows = array();
		if(!empty($type))
		{
			$user_ip = bindec(decbin(ip2long(real_ip())));
			$sql = 'SELECT e_id,e_value FROM select_elements WHERE user_name = "'.$_SESSION['AName'].'" AND user_ip =  
			"'.$user_ip.'"  AND e_type = "'.$type.'" AND cat_id = '.$cat_id.' LIMIT 1 ';
			$rows = $GLOBALS['db']->getAll($sql);
		}
		return $rows[0];
	}
	/**
	* 根据网站类型得到相应的Product Support栏目左侧信息
	* @param   int         $web_type    网站类别
	* @param   int         $two_cat_id  二级分类id
	* @return  array
	*/
	function  get_category_info($web_type,$two_cat_id)
	{
		$recommend_id = $recommend_article = '';//推荐id以逗号隔开
		$sql =  'SELECT  topic_cat_id,recommend_article FROM support_right_recommend  WHERE p_cat_id = '.$two_cat_id;
		
		$rows = $GLOBALS['db']->getAll($sql);
		if(!empty($rows[0]['recommend_article']))//以下是获取推荐的文章
		{
			$sql = 'SELECT ar_id,ar_title  FROM  topic_article  WHERE FIND_IN_SET(ar_id,"'.$rows[0]['recommend_article'].'")';
			$result = $GLOBALS['db']->query($sql);
			while($row = $GLOBALS['db']->fetch_array($result))
			{
				$recommend_id.= empty($recommend_id)?$row['ar_id']:','.$row['ar_id'];
				$recommend_article.= empty($recommend_article)?$row['ar_title']:chr(13).$row['ar_title'];
			}
		}
		$sql = 'SELECT cat_title  FROM category WHERE cat_id = '.$two_cat_id;
		
		$cat_title = $GLOBALS['db']->getOne($sql);
		$sql = 'SELECT cat_title,cat_id FROM  category WHERE  cat_type =1 AND  cat_web = '.$web_type.'
		AND  is_delete =0 AND  cat_series =3  ORDER BY  cat_title  ASC  ';
		
		$info = $this->support_right_recommend_m->get_info($sql);
		
		return array('two_cat_title' => $cat_title ,'topic_cat_id' => $rows[0]['topic_cat_id'] ,
		'recommend_id' => $recommend_id,'recommend_article' => $recommend_article,
		'date' => $info);
	}
	/**
	* 得到列表faq_info_new信息
	*
	* @param   array      $search_array   搜索数组
	* @return  array
	*/
	function  get_article_list($search_array = null)
	{
		$info = $check_row = array();
		$rows = $this->get_select_elements_id('support_right',$search_array['topic_cat_id']);
		
		if(empty($rows['e_value']))
		{
			$check_row = empty($search_array['recommend_id'])?array():explode(',',$search_array['recommend_id']);
			$check_strid = $search_array['recommend_id'];
		}else
		{
			$check_row = explode(',',$rows['e_value']);
			$check_strid = $rows['e_value'];
		}
		
		
		$data = $this->get_search_condition($search_array,'search_info');
		$sql = 'SELECT ar_id , ar_title , ar_subtitle  FROM topic_article WHERE is_delete = 0 '.$data['where_str'];
		
		$result = $GLOBALS['db']->query($sql);
		
		while($row = $GLOBALS['db']->fetch_array($result))
			{
			$check_str = '';
			if(in_array($row['ar_id'],$check_row))
			{
				$check_str = 'checked="checked"';
			
			}
			$row['checkbox'] =  '<input type="checkbox" name="check_article_list" '.$check_str.' value="'.$row['ar_id'].'" 
			title="'.$row['ar_title'].'">';
			$info[] =  $row;
		}
		
		return array('info'=>$info,'check_strid'=>$check_strid);
	}
	/**
	* 得到在搜索的条件
	*
	* @param   array        $search_array   搜索条件
	* @return  array
	*/
	function get_search_condition($search_array,$type = '')
	{
		$url_str = '&url_page=1'; $where_str = '';
		if(!empty($search_array['ar_id']))
		{
			$where_str .= ' AND  ar_id = '.$search_array['ar_id'];
			$url_str.= '&re_ar_id ='.$search_array['ar_id'];
		}else
		{
			if($search_array['type'] == 'search')//代表为搜索
			{
				if(!empty($search_array['ar_title']))//文章标题
				{
				$where_str .= " AND ar_title  LIKE '%{$search_array['ar_title']}%'";
				$url_str.= '&re_ar_title='.urlencode($search_array['ar_title']);
				}
				if($search_array['is_show'] != -1)//当搜索为已选
				{
				$where_str .= $search_array['is_show'] == 0?' AND  !FIND_IN_SET(ar_id,"'.$search_array['recommend_id'].'") ':
				' AND  FIND_IN_SET(ar_id,"'.$search_array['recommend_id'].'") ';
				$url_str.= '&is_show ='.$search_array['is_show'];
				}
			}
			if(!empty($search_array['web_type']))//网站类别
			{
			$where_str .= ' AND  web_type = '.$search_array['web_type'];
			$url_str.= '&web_type ='.$search_array['web_type'];
			}
			if(!empty($search_array['topic_cat_id']))//文章主分类
			{
			$where_str .= ' AND ar_cat_id = '.$search_array['topic_cat_id'];
			$url_str.= '&topic_cat_id ='.$search_array['topic_cat_id'];
			}
		}
		if($type == 'get_url')
		{
			return array('where_str' => $where_str,'url_str' => $url_str);
		}else
		{
			return array('where_str' => $where_str);
		}
	
	}



}
?>